using System;
using System.IO;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using My.IdlConverter.Core;
using My.IdlConverter.Core.Parsers;

namespace My.IdlConverter.Analysis
{
    using ArrayHelper = My.IdlConverter.Core.Util.ArrayHelper;

    public class InterfaceAnalysisReporter : AnalysisReporter,IInterfaceAnalysisReporter
    {
        private IList<string> _addedInterface = new List<string>();
        private IDictionary<string, StringCollection> _difMethods = new Dictionary<string, StringCollection>();

        public InterfaceAnalysisReporter(StreamWriter writer)
            : base(writer)
        {
        }

        public virtual void Compare(IList<Interface> oldInterfaceList, IList<Interface> newInterfaceList)
        {
            foreach (Interface nextNewInterface in newInterfaceList)
            {
                int index = ArrayHelper.IndexOf<Interface>(oldInterfaceList,nextNewInterface);
                if (index == -1)
                    _addedInterface.Add(nextNewInterface.Name);
                else 
                {
                    StringCollection newMethods = new StringCollection();
                    Interface oldInterfaceObj = oldInterfaceList[index];
                    foreach(Method nextNewMethod in nextNewInterface.Methods)
                    {
                        Method findedMethod = FindMethodByName(oldInterfaceObj.Methods,nextNewMethod.Name);
                        if (findedMethod == null)
                            newMethods.Add(nextNewMethod.Name);
                    }
                    if (newMethods.Count != 0)
                        _difMethods.Add(oldInterfaceObj.Name,newMethods);
                }
            }
        }

        public override void Report()
        {
            if (Writer == null)
                throw new ArgumentNullException("OutStream");

            Writer.WriteLine("-------------------Added Interface---------------------------------------");
            for(int i=0; i < _addedInterface.Count; ++i)
            {
                Writer.WriteLine(String.Format("\t{0}.{1}", i, _addedInterface[i]));
            }
            Writer.Flush();
            Writer.WriteLine("-------------------Added Methods-----------------------------------------");
            foreach(KeyValuePair<string,StringCollection> nextPair in _difMethods)
            {
                Writer.WriteLine(String.Format("\t{0}",nextPair.Key));
                foreach(string nextMethod in nextPair.Value)
                {
                    Writer.WriteLine(String.Format("\t\t{0}",nextMethod));
                }
            }
            Writer.Flush();
        }

        private Method FindMethodByName(IList<Method> methods, string name)
        {
            name = name.Trim();

            foreach (Method nextMethod in methods)
            {
                if (nextMethod.Name.Trim().Equals(name))
                    return nextMethod;
            }
            return null;
        }
    }
}
